package com.example.yls.demoa;

import android.graphics.Color;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.widget.Toast;

import com.baidu.location.BDAbstractLocationListener;
import com.baidu.location.BDLocation;
import com.baidu.location.LocationClient;
import com.baidu.location.LocationClientOption;
import com.baidu.mapapi.map.ArcOptions;
import com.baidu.mapapi.map.BaiduMap;
import com.baidu.mapapi.map.BitmapDescriptor;
import com.baidu.mapapi.map.BitmapDescriptorFactory;
import com.baidu.mapapi.map.CircleOptions;
import com.baidu.mapapi.map.LogoPosition;
import com.baidu.mapapi.map.MapPoi;
import com.baidu.mapapi.map.MapStatusUpdateFactory;
import com.baidu.mapapi.map.MapView;
import com.baidu.mapapi.map.Marker;
import com.baidu.mapapi.map.MarkerOptions;
import com.baidu.mapapi.map.MyLocationConfiguration;
import com.baidu.mapapi.map.MyLocationData;
import com.baidu.mapapi.map.Overlay;
import com.baidu.mapapi.map.OverlayOptions;
import com.baidu.mapapi.map.PolylineOptions;
import com.baidu.mapapi.map.Stroke;
import com.baidu.mapapi.map.TextOptions;
import com.baidu.mapapi.map.UiSettings;
import com.baidu.mapapi.model.LatLng;

import java.util.ArrayList;
import java.util.List;

public class MainActivity extends AppCompatActivity {
private MapView mMapView;
private BaiduMap mBaiduMap;
private LocationClient mLocationClient;
    private UiSettings mUiSettings;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        initView();
        setBaiduMap();
    }

    private void setBaiduMap() {
        mBaiduMap.setMyLocationEnabled(true);
        mLocationClient = new LocationClient(this);
        MyLocationConfiguration myLocationConfiguration=new MyLocationConfiguration(
                MyLocationConfiguration.LocationMode.FOLLOWING,true,
                BitmapDescriptorFactory.fromResource(R.drawable.ioc),
                0xAAFFFF88,
                0xAA00FF00
        );
        mBaiduMap.setMyLocationConfiguration(myLocationConfiguration);
//通过LocationClientOption设置LocationClient相关参数
        LocationClientOption option = new LocationClientOption();
        option.setOpenGps(true); // 打开gps
        option.setCoorType("bd09ll"); // 设置坐标类型
        option.setScanSpan(1000);
//设置locationClientOption
        mLocationClient.setLocOption(option);
//注册LocationListener监听器
        MyLocationListener myLocationListener = new MyLocationListener();
        mLocationClient.registerLocationListener(myLocationListener);
//开启地图定位图层
        mLocationClient.start();


        mMapView.setLogoPosition(LogoPosition.logoPostionCenterTop);
        mBaiduMap.setPadding(20, 20, 20, 20);
        mUiSettings = mBaiduMap.getUiSettings();

        //指南针
        mUiSettings.setCompassEnabled(false);
        //缩放图标
        mMapView.showZoomControls(true);
        //缩放
        mMapView.showScaleControl(true);

        //缩放级别
        //直接缩放至缩放级别16
        mBaiduMap.setMapStatus(MapStatusUpdateFactory.zoomTo(16));
//使缩放级别增大一级
        mBaiduMap.setMapStatus(MapStatusUpdateFactory.zoomIn());
//使缩放级别减小一级
        mBaiduMap.setMapStatus(MapStatusUpdateFactory.zoomOut());
//在当前的缩放级别上增加或减小具体的缩放级别
// 4 在当前的缩放级别上增加4
        mBaiduMap.setMapStatus(MapStatusUpdateFactory.zoomBy(4));
// -4 在当前的缩放级别上减小4
        mBaiduMap.setMapStatus(MapStatusUpdateFactory.zoomBy(-4));
        mBaiduMap.setOnMapClickListener(new BaiduMap.OnMapClickListener() {
            @Override
            public void onMapClick(LatLng latLng) {
                Toast.makeText(MainActivity.this, "单击 纬度"+latLng.latitude
                        +"经度"+latLng.longitude, Toast.LENGTH_SHORT).show();
            }

            @Override
            public boolean onMapPoiClick(MapPoi mapPoi) {
                Toast.makeText(MainActivity.this, "MapPoi单击"+mapPoi.getName()+
                        "坐标"+mapPoi.getPosition().toString(), Toast.LENGTH_SHORT).show();
                return false;
            }
        });

        mBaiduMap.setOnMapDoubleClickListener(new BaiduMap.OnMapDoubleClickListener() {
            @Override
            public void onMapDoubleClick(LatLng latLng) {
                Toast.makeText(MainActivity.this, "单击 纬度"+latLng.latitude
                        +"经度"+latLng.longitude, Toast.LENGTH_SHORT).show();
            }
        });

        mBaiduMap.setOnMapLongClickListener(new BaiduMap.OnMapLongClickListener() {
            @Override
            public void onMapLongClick(LatLng latLng) {
                Toast.makeText(MainActivity.this, "单击 纬度"+latLng.latitude
                        +"经度"+latLng.longitude, Toast.LENGTH_SHORT).show();
                //drawMark(latLng);
             //  drawPolyLine(latLng);
               //drawArc(latLng);
                //drawCircle(latLng);
                //drawTest(latLng);
                drawTest2(latLng);
            }
        });

        mBaiduMap.setOnMarkerClickListener(new BaiduMap.OnMarkerClickListener() {
            @Override
            public boolean onMarkerClick(Marker marker) {
                Toast.makeText(MainActivity.this, marker.getTitle(), Toast.LENGTH_SHORT).show();
                return false;
            }
        });
    }

    private void drawTest2(LatLng latLng) {




    }

    private void drawTest(LatLng latLng) {

        LatLng llText = latLng;

//构建TextOptions对象
        OverlayOptions mTextOptions = new TextOptions()
                .text("百度地图SDK") //文字内容
                .bgColor(0xAAFFFF00) //背景色
                .fontSize(24) //字号
                .fontColor(0xFFFF00FF) //文字颜色
                .rotate(-30) //旋转角度
                .position(llText);

//在地图上显示文字覆盖物
         mBaiduMap.addOverlay(mTextOptions);

    }

    private void drawCircle(LatLng latLng) {
        //圆心位置
        LatLng center = latLng;

//构造CircleOptions对象
        CircleOptions mCircleOptions = new CircleOptions().center(center)
                .radius(1400)
                .fillColor(0xAA0000FF) //填充颜色
                .stroke(new Stroke(5, 0xAA00ff00)); //边框宽和边框颜色

//在地图上显示圆
        Overlay mCircle = mBaiduMap.addOverlay(mCircleOptions);

    }

    private void drawArc(LatLng latLng) {
        // 添加弧线坐标数据
        LatLng p1 = latLng;//起点
        LatLng p2 = new LatLng(latLng.latitude-0.1, latLng.longitude+0.1);//中间点
        LatLng p3 = new LatLng(latLng.latitude, latLng.longitude+0.2);//终点

//构造ArcOptions对象
        OverlayOptions mArcOptions = new ArcOptions()
                .color(Color.RED)
                .width(10)
                .points(p1, p2, p3);

//在地图上显示弧线
        mBaiduMap.addOverlay(mArcOptions);


    }

    private void drawPolyLine(LatLng latLng) {
        LatLng p1 = latLng;
        LatLng p2 = new LatLng(latLng.latitude-0.1, latLng.longitude+0.1);
        LatLng p3 = new LatLng(latLng.latitude+0.1, latLng.longitude+0.2);
        List<LatLng> points = new ArrayList<LatLng>();
        points.add(p1);
        points.add(p2);
        points.add(p3);

//设置折线的属性
        OverlayOptions mOverlayOptions = new PolylineOptions()
                .width(10)
                .color(0xAAFF0000)
                .points(points);
//在地图上绘制折线
//mPloyline 折线对象
         mBaiduMap.addOverlay(mOverlayOptions);
    }

    private void drawMark(LatLng latLng) {
        LatLng point = latLng ;
//构建Marker图标
        BitmapDescriptor bitmap = BitmapDescriptorFactory
                .fromResource(R.drawable.mark);
//构建MarkerOption，用于在地图上添加Marker
        OverlayOptions option = new MarkerOptions()
                .position(point)
                .icon(bitmap);
//在地图上添加Marker，并显示
        mBaiduMap.addOverlay(option);

    }

    @Override
    protected void onResume() {
        super.onResume();
        //在activity执行onResume时执行mMapView. onResume ()，实现地图生命周期管理
        mMapView.onResume();
    }
    @Override
    protected void onPause() {
        super.onPause();
        //在activity执行onPause时执行mMapView. onPause ()，实现地图生命周期管理
        mMapView.onPause();
    }
    @Override
    protected void onDestroy() {
        super.onDestroy();
        //在activity执行onDestroy时执行mMapView.onDestroy()，实现地图生命周期管理
        mMapView.onDestroy();
        mLocationClient.stop();
        mBaiduMap.setMyLocationEnabled(false);
    }

    private void initView() {
        mMapView=findViewById(R.id.bmapView);
        mBaiduMap=mMapView.getMap();
    }

    class MyLocationListener extends BDAbstractLocationListener {
        @Override
        public void onReceiveLocation(BDLocation location) {
            //mapView 销毁后不在处理新接收的位置
            if (location == null || mMapView == null){
                return;
            }
            MyLocationData locData = new MyLocationData.Builder()
                    .accuracy(location.getRadius())
                    // 此处设置开发者获取到的方向信息，顺时针0-360
                    .direction(location.getDirection()).latitude(location.getLatitude())
                    .longitude(location.getLongitude()).build();
            mBaiduMap.setMyLocationData(locData);
        }
    }
}
